package dataAccessLayer.tests;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;


import junit.framework.Test;

import org.eclipse.hyades.test.common.junit.DefaultTestArbiter;
import org.eclipse.hyades.test.common.junit.HyadesTestCase;
import org.eclipse.hyades.test.common.junit.HyadesTestSuite;

import dataAccessLayer.dataBase.DBManager;
import dataAccessLayer.factoryMySQL.daos.DAOcustomersMySQL;
import dataAccessLayer.visualObject.VOcustomer;

/**
 * Generated code for the test suite <b>TestDAOcustomersMySQL</b> located at
 * <i>/Pastry/src/dataAccessLayer/tests/TestDAOcustomersMySQL.testsuite</i>.
 *
 * esta clase tiene como fin probar los metodos de la clase DAOcustomersMySQL
 */
public class TestDAOcustomersMySQL extends HyadesTestCase {
	/**
	 * Constructor for TestDAOcustomersMySQL.
	 * @param name
	 */
	private DBManager manager = new DBManager(ResourceBundle.getBundle("dataAccessLayer.dataAccess").getString("localConnectionType"));
	private DAOcustomersMySQL DAOcustomers = new DAOcustomersMySQL(manager);
	
	public TestDAOcustomersMySQL(String name) {
		super(name);
	}

	/**
	 * Returns the JUnit test suite that implements the <b>TestDAOcustomersMySQL</b> definition.
	 */
	public static Test suite() {
		HyadesTestSuite testDAOcustomersMySQL = new HyadesTestSuite(
				"TestDAOcustomersMySQL");
		testDAOcustomersMySQL.setArbiter(DefaultTestArbiter.INSTANCE).setId(
				"A1DD967EB6AEAF10C09AE13764653466");
		testDAOcustomersMySQL.addTest(new TestDAOcustomersMySQL(
				"testInsertRecipe").setId("A1DD967EC9563A20C09AE13764653466")
				.setTestInvocationId("A1DD9680A52182C0C09AE13764653466"));
		testDAOcustomersMySQL.addTest(new TestDAOcustomersMySQL(
				"testUpdateCustomer").setId("A1DD9680BF5916D0C09AE13764653466")
				.setTestInvocationId("A1DD96829A764520C09AE13764653466"));
		testDAOcustomersMySQL.addTest(new TestDAOcustomersMySQL(
				"testRemoveCustomer").setId("A1DD9682B7C55260C09AE13764653466")
				.setTestInvocationId("A1DD9682C208BB90C09AE13764653466"));
		testDAOcustomersMySQL.addTest(new TestDAOcustomersMySQL(
				"testListCustomers").setId("A1DD96832EEEFF80C09AE13764653466")
				.setTestInvocationId("A1DD968339EC42D0C09AE13764653466"));
		testDAOcustomersMySQL.addTest(new TestDAOcustomersMySQL(
				"testSearchCustomerByCode").setId(
				"A1DD96836D27A5E0C09AE13764653466").setTestInvocationId(
				"A1DD9683768FCA40C09AE13764653466"));
		testDAOcustomersMySQL.addTest(new TestDAOcustomersMySQL(
				"testSearchCustomerByName").setId(
				"A1DD9683C4325CE0C09AE13764653466").setTestInvocationId(
				"A1DD9683D9113A50C09AE13764653466"));
		return testDAOcustomersMySQL;
	}

	/**
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
	}

	/**
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
	}

	/**
	 * testInsertRecipe
	 * @throws Exception
	 */
	public void testInsertRecipe() throws Exception
	{
		VOcustomer vOcustomer= new VOcustomer();
		vOcustomer.setName("la gran pasteleria");
		vOcustomer.setCity(1);
		vOcustomer.setTelephone("7440132");
		vOcustomer.setAddress("calle 20 # 14-19");
		
		this.DAOcustomers.insertCustomer(vOcustomer);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		String name="";
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM customers ORDER BY code DESC LIMIT 1");
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				name=resultSet.getString(2);
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		
		assertEquals(vOcustomer.getName(), name);
	}

	/**
	 * testUpdateCustomer
	 * @throws Exception
	 */
	public void testUpdateCustomer() throws Exception 
	{
		VOcustomer vOcustomer= new VOcustomer();
		vOcustomer.setName("la mega pasteleria");
		vOcustomer.setCity(1);
		vOcustomer.setTelephone("7440130");
		vOcustomer.setAddress("calle 10 # 15-19");
		vOcustomer.setCode(2);
		
		this.DAOcustomers.updateCustomer(vOcustomer);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		String name="";
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM customers where code=?");
			preparedStatement.setInt(1,vOcustomer.getCode());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				name = resultSet.getString(2);
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		
		assertEquals(vOcustomer.getName(), name);
	}

	/**
	 * testRemoveCustomer
	 * @throws Exception
	 */
	public void testRemoveCustomer() throws Exception
	{
		VOcustomer vOcustomer= new VOcustomer();
		vOcustomer.setCode(4);
		
		this.DAOcustomers.removeCustomer(vOcustomer);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		VOcustomer searchVoCustomer = new VOcustomer();
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM customers where code=?");
			preparedStatement.setInt(1,vOcustomer.getCode());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				searchVoCustomer.setCode(resultSet.getInt(1));
				searchVoCustomer.setName(resultSet.getString(2));
			}
			else
			{
				searchVoCustomer=null;
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		assertEquals(searchVoCustomer,null );
	}

	/**
	 * testListCustomers
	 * @throws Exception
	 */
	public void testListCustomers() throws Exception
	{
		ArrayList<VOcustomer> search = this.DAOcustomers.listCustomers();
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		
		int count=0;
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM customers");
			resultSet = preparedStatement.executeQuery();
			
			while(resultSet.next())
			{
				count++;
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		assertEquals(search.size(),count );
	}

	/**
	 * testSearchCustomerByCode
	 * @throws Exception
	 */
	public void testSearchCustomerByCode() throws Exception 
	{
		VOcustomer vOcustomer= new VOcustomer();
		vOcustomer.setCode(5);
		

		VOcustomer search = this.DAOcustomers.searchCustomerByCode(vOcustomer);
		
		
		assertEquals(vOcustomer.getCode(),search.getCode());
	}

	/**
	 * testSearchCustomerByName
	 * @throws Exception
	 */
	public void testSearchCustomerByName() throws Exception 
	{
		VOcustomer vOcustomer = new VOcustomer();
		vOcustomer.setName("andres");
		

		VOcustomer search = this.DAOcustomers.searchCustomerByName(vOcustomer);
		
		
		assertEquals(vOcustomer.getName(),search.getName());
	}

}
